﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace NCKH.Admin
{
    public partial class suatintuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hientennhomtin();
                hientendanhmuctin();
                if (Request.QueryString["id"] != null)
                {
                    int NewsID = Convert.ToInt32(Request.QueryString["id"]);
                    hientintuc(NewsID);
                }
            }
        }

        protected void hientintuc(int NewsID)
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string select_danhmuc = "SELECT Manoidungtin, Tieude, DANHMUCTIN.Madanhmuctin, Tendanhmuctin, Tomtat, Ngaytao, Noidung, Tag, Image, bhienthi, trangthaitin FROM NOIDUNGTIN, DANHMUCTIN WHERE NOIDUNGTIN.Madanhmuctin = DANHMUCTIN.Madanhmuctin and Manoidungtin='" + NewsID + "'";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(select_danhmuc, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows && rd.Read())
                        {
                            txtmatin.Text = rd["Manoidungtin"].ToString();
                            txttieude.Text = rd["Tieude"].ToString();
                            ddldanhmuctin.SelectedValue = rd["Madanhmuctin"].ToString();
                            txttomtat.Text = rd["Tomtat"].ToString();
                            txtnoidung.Text = rd["Noidung"].ToString();
                            txtTag.Text = rd["Tag"].ToString();
                            //fileupload1.FileName = rd["Image"].ToString();
                            drhienthi.SelectedValue = rd["bhienthi"].ToString();
                            drdtrangthai.SelectedValue = rd["trangthaitin"].ToString();
                            btnsua.CommandArgument = NewsID.ToString();
                        }
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }
        protected void hientennhomtin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlselect = "SELECT Manhomtin, Tennhomtin FROM NHOMTIN";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(sqlselect, cnn))
                {
                    cnn.Open();
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows)
                            while (rd.Read())
                            {
                                ddlnhomtin.Items.Add(
                                    new ListItem(rd["Tennhomtin"].ToString(), rd["Manhomtin"].ToString()));
                            }
                        else
                            btnsua.Enabled = false;
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }

        protected void hientendanhmuctin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlselect = "SELECT Madanhmuctin, Tendanhmuctin FROM DANHMUCTIN";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(sqlselect, cnn))
                {
                    cnn.Open();
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows)
                            while (rd.Read())
                            {
                                ddldanhmuctin.Items.Add(
                                    new ListItem(rd["Tendanhmuctin"].ToString(), rd["Madanhmuctin"].ToString()));
                            }
                        else
                            btnsua.Enabled = false;
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }
        protected bool kiemtratieude()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string ktten = "SELECT Tieude From NOIDUNGTIN WHERE Tieude=N'" + txttieude.Text + "'";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(ktten, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows && rd.Read())
                        {
                            lbltb.Text = "Tin này đã tồn tại";
                            // Response.Write("<script>alert('Tên đã bị trùng')</script>");
                            return true;
                        }
                        else
                        {
                            lbltb.Text = "";
                            return false;
                        }
                        rd.Close();
                    }
                }
                cnn.Close();
            }
        }



        protected void btnsua_Click(object sender, EventArgs e)
        {
            int @Matintuc = Convert.ToInt32(btnsua.CommandArgument);
            int catID = Convert.ToInt32(ddldanhmuctin.SelectedValue);
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            //string update = "UPDATE NOIDUNGTIN SET Manoidungtin='" + catID + "', Tendanhmuctin=@Tendanhmuctin Where Madanhmuctin='" + @Madanhmuctin + "'";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand("update_tintuc", cnn))
                {

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("Manoidungtin", btnsua.CommandArgument);
                    cmd.Parameters.AddWithValue("Madanhmuctin", catID);
                    cmd.Parameters.AddWithValue("Tieude", txttieude.Text);
                    cmd.Parameters.AddWithValue("Tomtat", txttomtat.Text);
                    cmd.Parameters.AddWithValue("Noidung", txtnoidung.Text);
                    cmd.Parameters.AddWithValue("Ngaytao", DateTime.Now);
                    cmd.Parameters.AddWithValue("Tag", txtTag.Text);
                    cmd.Parameters.AddWithValue("Image", "~/images/" + fileupload1.FileName);
                    fileupload1.SaveAs(Server.MapPath("~/images/" + fileupload1.FileName));
                    cmd.Parameters.AddWithValue("bhienthi", drhienthi.SelectedValue);
                    cmd.Parameters.AddWithValue("trangthaitin", drdtrangthai.SelectedValue);
                    //cmd.Parameters.AddWithValue("@Tendanhmuctin", txttendanhmuc.Text);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                }
            }
            Response.Redirect("tintuc.aspx");
        }

        protected void btnback_Click(object sender, EventArgs e)
        {
            Response.Redirect("tintuc.aspx");
        }

        protected void txttomtat_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtnoidung_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtTag_TextChanged(object sender, EventArgs e)
        {

        }

        protected void drhienthi_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void txtmatin_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ddlnhomtin_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddldanhmuctin_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void txttieude_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txttomtat_TextChanged1(object sender, EventArgs e)
        {

        }

        protected void txtnoidung_TextChanged1(object sender, EventArgs e)
        {

        }
    }
}